package cn.vendsystem.com.utils;

import cn.vendsystem.com.properties.JwtProperties;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import jakarta.annotation.PostConstruct;

import java.util.Date;
import java.util.Map;

public class JwtUtil {

    private static String key = "eleven";
    private static long expireMinutes = 30;

//    private final JwtProperties jwtProperties;
//
//    public JwtUtil(JwtProperties jwtProperties) {
//        this.jwtProperties = jwtProperties;
//    }
//
//    @PostConstruct
//    public void init() {
//        key = jwtProperties.getSecret();
//        expireMinutes = jwtProperties.getExpireMinutes();
//    }

    // 接收业务数据,生成token并返回
    public static String genToken(Map<String, Object> claims) {
        return JWT.create()
                .withClaim("claims", claims)
                .withExpiresAt(new Date(System.currentTimeMillis() + 1000L * 60 * expireMinutes))
                .sign(Algorithm.HMAC256(key));
    }

    // 接收token,验证token,并返回业务数据
    public static Map<String, Object> parseToken(String token) {
        return JWT.require(Algorithm.HMAC256(key))
                .build()
                .verify(token)
                .getClaim("claims")
                .asMap();
    }

}
